namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class IndicatorMassIndex : BaseIndicator
    {
        private string ahlDevideBhlField = ("MASS.AhlDevideBhlField" + Common.GetGuid());
        private string ahlField = ("MASS.AhlField." + Common.GetGuid());
        private string bhlField = ("MASS.BhlField." + Common.GetGuid());
        private string diffField = ("MASS.DiffField" + Common.GetGuid());
        private LineShape tlsMass;

        public IndicatorMassIndex()
        {
            base.IndType = IndicatorType.MASS;
            base.IndName = "MASS";
            base.IndDesc = "梅斯线";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 25.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "N2", 9.0));
            base.SourceDic["High"] = "";
            base.SourceDic["Low"] = "";
            base.fields = new string[] { "MASS" };
        }

        public override bool Contains(object obj)
        {
            return (obj == this.tlsMass);
        }

        public override double[] Get(int r)
        {
            double num = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["High"]]);
            double num2 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Low"]]);
            double curValue = num - num2;
            double num4 = Common.CalculateExponentialMovingAvg(this.ahlField, this.diffField, (int) base.IndPeriod[1].ParamValue, curValue, base.Crab.DataSource, r);
            double num5 = Common.CalculateExponentialMovingAvg(this.bhlField, this.ahlField, (int) base.IndPeriod[1].ParamValue, num4, base.Crab.DataSource, r);
            double num6 = (num5 != 0.0) ? (num4 / num5) : 0.0;
            int num7 = r - (((int) base.IndPeriod[0].ParamValue) - 1);
            if (num7 < 0)
            {
                num7 = 0;
            }
            double num8 = num6;
            for (int i = r - 1; i >= num7; i--)
            {
                num8 += Convert.ToDouble(base.Crab.DataSource.Rows[i][this.ahlDevideBhlField]);
            }
            return new double[] { curValue, num4, num5, num6, num8 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsMass };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsMass);
            base.Crab.DataSource.Columns.Remove(this.AhlField);
            base.Crab.DataSource.Columns.Remove(this.BhlField);
            base.Crab.DataSource.Columns.Remove(this.DiffField);
            base.Crab.DataSource.Columns.Remove(this.AhlDevideBhlField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.DiffField, numArray[0], r);
                        base.Crab.SetValue(this.AhlField, numArray[1], r);
                        base.Crab.SetValue(this.BhlField, numArray[2], r);
                        base.Crab.SetValue(this.AhlDevideBhlField, numArray[3], r);
                        base.Crab.SetValue(this.TlsMass.Name, numArray[4], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            base.SourceDic["High"] = source[0];
            base.SourceDic["Low"] = source[1];
            base.IndPeriod[1].ParamValue = periods[0];
            base.IndPeriod[0].ParamValue = periods[1];
            this.TlsMass = base.Crab.AddLine(fields[0], panelID);
            base.Crab.DataSource.Columns.Add(this.AhlField);
            base.Crab.DataSource.Columns.Add(this.BhlField);
            base.Crab.DataSource.Columns.Add(this.DiffField);
            base.Crab.DataSource.Columns.Add(this.AhlDevideBhlField);
        }

        public string AhlDevideBhlField
        {
            get
            {
                return this.ahlDevideBhlField;
            }
            set
            {
                this.ahlDevideBhlField = value;
            }
        }

        public string AhlField
        {
            get
            {
                return this.ahlField;
            }
            set
            {
                this.ahlField = value;
            }
        }

        public string BhlField
        {
            get
            {
                return this.bhlField;
            }
            set
            {
                this.bhlField = value;
            }
        }

        public string DiffField
        {
            get
            {
                return this.diffField;
            }
            set
            {
                this.diffField = value;
            }
        }

        public LineShape TlsMass
        {
            get
            {
                return this.tlsMass;
            }
            set
            {
                this.tlsMass = value;
            }
        }
    }
}

